package model

import (
	"fmt"
	"gowork/productionInformation/utils"
	"strings"
	"time"
)

type ProductionInformation struct {
	Id int64 `xorm:"pk autoincr"`
	RawMaterialId int64
	RawMaterialName string
	CommodityCode string
	PurchaseNumber int64
	BusinessDate string
	Company string
	Manufacturer string
	QualityInspection int
	State int
	ProductionTapList []*ProductionTap `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ProductionTap struct {
	Id int64 `xorm:"pk autoincr"`
	ProductionInformationId int64
	ProductionBatchList []*ProductionBatch `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ProductionBatch struct {
	Id int64 `xorm:"pk autoincr"`
	ProductionTapId int64
	BatchNumber string
	ManufactureDate string
	ProductValidity string
	Number int64
	QualityInspector string
	QualityInspectionReport string
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (productionInformation *ProductionInformation)InsertProductionInformation(pi *ProductionInformation)(map[string]interface{}, error){

	w := new(utils.Worker)

	for _, pt := range pi.ProductionTapList{
		for _, pb := range pt.ProductionBatchList{
			var countBatchNumber int = 0
			for _, ptv := range pi.ProductionTapList {
				for _, pbv := range ptv.ProductionBatchList {
					if pbv.BatchNumber == pb.BatchNumber {
						countBatchNumber += 1
					}
				}
			}
			if countBatchNumber > 1{
				return map[string]interface{}{"message": "生产批号重复","code":400}, nil
			}
		}
	}

	for _, pt := range pi.ProductionTapList{
		for _, pb := range pt.ProductionBatchList{
			for _, d := range strings.Split(pb.ManufactureDate, ","){
				var countManufactureDate int = 0
				for _, ptv := range pi.ProductionTapList {
					for _, pbv := range ptv.ProductionBatchList {
						for _, dv := range strings.Split(pbv.ManufactureDate, ",") {
							if d == dv {
								countManufactureDate += 1
							}
						}
					}
				}
				if countManufactureDate > 1{
					return map[string]interface{}{"message": "生产日期重复","code":400}, nil
				}
			}
		}
	}

	var ptList []*ProductionTap
	x.Where("production_information_id = ?", pi.Id).Find(&ptList)
	for _, pt := range ptList{
		x.Where("production_tap_id = ?", pt.Id).Delete(new(ProductionBatch))
	}
	x.Where("production_information_id = ?", pi.Id).Delete(new(ProductionTap))

	for _, ptv := range pi.ProductionTapList{
		ptv.Id = w.GetId()
		ptv.ProductionInformationId = pi.Id
		ptv.CreatedAt = time.Now()
		ptv.UpdatedAt = time.Now()
		x.Insert(ptv)
		for _, pb := range ptv.ProductionBatchList {
			pb.Id = w.GetId()
			pb.ProductionTapId = ptv.Id
			pb.CreatedAt = time.Now()
			pb.UpdatedAt = time.Now()
			x.Insert(pb)
		}
	}

	return map[string]interface{}{"message": "成功","code":200}, nil
}

func (productionInformation *ProductionInformation)QueryProductionInformation(piId int64)([]*ProductionTap, error){

	fmt.Println("piId：", piId)

	var ptList []*ProductionTap
	x.Where("production_information_id = ?", piId).Find(&ptList)
	for _, pt := range ptList{
		x.Where("production_tap_id = ?", pt.Id).Find(&pt.ProductionBatchList)
	}

	fmt.Println("ptList：", ptList)
	return ptList, nil
}